#include "PacketScene.h"
#include "../../../net/NCDefines.h"
#include "../../../net/NetController.h"
#include "../../../model/proxy/ItemProxy.h"
#include "../../../model/proxy/UserProxy.h"
#include "../EquipItemLoader.h"
PackTab PacketScene::msCurTab = PACK_TAB_EQUIP;
PacketScene::PacketScene(void)
{
}

PacketScene::~PacketScene(void)
{
}

bool PacketScene::init()
{
	return true;
}

void PacketScene::onEquipTabClick(CCObject * pSender, CCControlEvent pCCControlEvent)
{
    changeTab(PACK_TAB_EQUIP);
}

void PacketScene::onPacketTabClick( CCObject * pSender, CCControlEvent pCCControlEvent )
{
	 changeTab(PACK_TAB_PACKET);
}

void PacketScene::onSellTabClick( CCObject * pSender, CCControlEvent pCCControlEvent )
{
    if(msCurTab == PACK_TAB_EQUIP) return;
    vector<int>& sellList = ItemProxy::getSingletonPtr()->sellList;
    sellList.clear();
    vector<ListItem*>::iterator iter = mCurList.begin();
    while(iter != mCurList.end())
    {
        EquipItem* equipItem = (EquipItem*)(*iter);
        if(equipItem->isSelect())
        {
            EquipInfo* equipInfo = (EquipInfo*)equipItem->getData();
            sellList.push_back(equipInfo->index);
        }
        iter++;
    }
    if(sellList.size() > 0)
    {
        NetController::getSingletonPtr()->sellEquipage(sellList);
        
        //test
//        ItemProxy::getSingletonPtr()->sellItem(sellList);
//        UserProxy::getSingletonPtr()->userVO.gold += 100;
//        int sellCoin = 100;
//        Post_Net_Notification(kNCSellEquipage, (CCObject*)&sellCoin);
    }
}

void PacketScene::refresh()
{
	changeTab(msCurTab);
}

SEL_MenuHandler PacketScene::onResolveCCBCCMenuItemSelector(CCObject * pTarget, CCString * pSelectorName )
{
	
	return NULL;
}

SEL_CCControlHandler PacketScene::onResolveCCBCCControlSelector( CCObject * pTarget, CCString * pSelectorName )
{
	CCB_SELECTORRESOLVER_CCCONTROL_GLUE(this, "onEquipTabClick", PacketScene::onEquipTabClick);
	CCB_SELECTORRESOLVER_CCCONTROL_GLUE(this, "onPacketTabClick", PacketScene::onPacketTabClick);
	CCB_SELECTORRESOLVER_CCCONTROL_GLUE(this, "onSellTabClick", PacketScene::onSellTabClick);
	return NULL;
}

bool PacketScene::onAssignCCBMemberVariable( CCObject * pTarget, CCString * pMemberVariableName, CCNode * pNode )
{
	return false;
}

void PacketScene::onEnter()
{
	State::onEnter();
	CCArray* nameList = CCArray::create(ccs(kNCLoadEquipage),ccs(kNCUnloadEquipage),ccs(kNCSellEquipage),ccs(kNCFusionEquipage),NULL);
	RegisterObservers(this, nameList, callfuncO_selector(PacketScene::_onNotification)); 
	refresh();
}

void PacketScene::onExit()
{
	State::onExit();
}

void PacketScene::onNodeLoaded( CCNode * pNode, CCNodeLoader * pNodeLoader )
{
	mScrollView = CCScrollView::create(CCSizeMake(320, 270));
	mScrollView->setDirection(CCScrollViewDirectionVertical);
	mScrollView->setPosition(ccp(0, 64));
	addChild(mScrollView);
}

void PacketScene::_onNotification( CCObject* object )
{
	CCLog("PacketScene::_onNotification");
	NotificationObserver* observer = (NotificationObserver*)object;
	std::string name(observer->getName());
	if (name == kNCLoadEquipage || name == kNCUnloadEquipage || name == kNCSellEquipage || name == kNCFusionEquipage)
	{
        refresh();
    }
}

void PacketScene::changeTab( PackTab tab )
{
	mCurList.clear();

	msCurTab = tab;

	if (tab == PACK_TAB_EQUIP)
	{
		RegisterLoader("EquipItem", EquipItemLoader::loader());
		EquipList equipList;
		ItemProxy::getSingletonPtr()->getPutOnList(equipList);
		CCPoint contentPos = ccp(0, equipList.size() * 74);
		CCLayer* content = CCLayer::create();
		content->setContentSize(CCSizeMake(320, contentPos.y));
		mScrollView->setContainer(content);
		mScrollView->setContentOffset(ccp(0, mScrollView->getViewSize().height-contentPos.y));
		for(int i = 0; i < equipList.size(); i++)
		{
			ListItem* item = (EquipItem*)ReadNode("EquipItem.ccbi", this);
			item->showCheckBox(false);
			item->setData(equipList[i]);
			item->refresh();
			item->setPosition(ccp(0, contentPos.y - (i+1) * 74));
			content->addChild(item);
			mCurList.push_back(item);
		}
	}
    else if(tab == PACK_TAB_PACKET)
    {
        RegisterLoader("EquipItem", EquipItemLoader::loader());
		EquipList equipList;
		ItemProxy::getSingletonPtr()->getTakeOffList(equipList);
		CCPoint contentPos = ccp(0, equipList.size() * 74);
		CCLayer* content = CCLayer::create();
		content->setContentSize(CCSizeMake(320, contentPos.y));
		mScrollView->setContainer(content);
		mScrollView->setContentOffset(ccp(0, mScrollView->getViewSize().height-contentPos.y));
		for(int i = 0; i < equipList.size(); i++)
		{
            ListItem* item = (EquipItem*)ReadNode("EquipItem.ccbi", this);
            item->showCheckBox(true);
            item->setData(equipList[i]);
            item->refresh();
            item->setPosition(ccp(0, contentPos.y - (i+1) * 74));
            content->addChild(item);
            mCurList.push_back(item);
		}
    }

}
